package com.pcbly.web.action.system;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.file.FileBean;
import com.pcbly.web.business.file.FileService;
import com.pcbly.web.components.utils.random.GUID;

public class UploadAction extends BaseAction<FileBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5091498606225275785L;

	private FileService fileService;

	private String contentType;

	private File upload;

	private String fileName;

	private String savePath;

	private String modelId;

	private String foreignKey;
	
	private String path; 

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public String getForeignKey() {
		return foreignKey;
	}

	public void setForeignKey(String foreignKey) {
		this.foreignKey = foreignKey;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String input() throws Exception {
		return super.index();
	}

	public String upload() throws Exception {
		if(log.isDebugEnabled()){
			log.debug("login upload.......");
		}
		String fileNewName = "F" + ((new GUID()).toString());

		fileNewName = fileNewName
				+fileName.substring(fileName.lastIndexOf("."), fileName
						.length());
		path=savePath + "/" + fileNewName;
		OutputStream fos = new FileOutputStream(getSavePath() + "\\"
				+ fileNewName); 
		InputStream fis = new FileInputStream(upload); 
		int len = 0;
		byte[] buffer = new byte[1024];
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		if (fos != null)
			fos.close(); 
		if (fis != null)
			fis.close(); 

		FileBean fileBean = new FileBean();
		fileBean.setActFile(fileName);
		fileBean.setRenameFile(fileNewName);
		fileBean.setId((new GUID()).toString());
		
		fileBean.setCreateDate(new Date());
		fileBean.setCreateUserId("089ED4F4-CEAD-6246-47AA-7599803D3EC5");// need
		// session
		fileBean.setFileName(fileNewName);
		fileBean.setFilePath(path);
		fileBean.setFileType(fileName.substring(fileName.lastIndexOf(".")+1, fileName
				.length()));
		fileBean.setModelId(modelId);
		fileBean.setFileLength(getUpload().length());
		fileBean.setForeignKey(foreignKey);

		fileService.saveFileBean(fileBean); 
		return SUCCESS;
	}

}
